// import CryptoJS from 'crypto-js';
import JSEncrypt from 'jsencrypt'

export default {
  //随机生成指定数量的16进制key
  generatekey(num) {
    let library = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    let key = "";
    for (var i = 0; i < num; i++) {
      let randomPoz = Math.floor(Math.random() * library.length);
      key += library.substring(randomPoz, randomPoz + 1);
    }
    return key;
  },

  //加密
  encrypt(word,key) {
    if (word == null || word === "") {
      // this.$message.error("无法对空字符串进行加密");
      return;
    }
	
	
	
		let encryptor = new JSEncrypt()  // 创建加密对象实例
	  //之前ssl生成的公钥，复制的时候要小心不要有空格
	  let pubKey = `MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAscs6gNXNSgV9pKKwriq7NETvEHKSd81alT7iQ0vwoJlnK32XsDWgxB+1YN943g0nDP2aLyTaanr7to6CpUhhu2C2oRzRG7x72bnrd5IVn1VbIvBQmA9rD8j6LpldguIHLN+fjcIgrpZSbxMNVqkMnYch93ywE081YVkreBD4bP+PqFEZb/LhmfwiYOIuZu01hbY7bJvfLoK/ni71xhYNa2OdF4uBX4seWNcRtiJZSblBJGiB+4AACfxp2X/noPFYJrhi908LG/uJm2OtyWVUtywc3/k+XiqRdpH/nwx1yJnE1vc7wv26WTweLXMy1QOR4b4tOx8DQk+pQuqg6CBb9wIDAQAB`
	  encryptor.setPublicKey(key || pubKey)//设置公钥
	  return encryptor.encrypt(word)  // 对内容进行加密
	  
	
	
	
  },
  crypEncrypt(word){},
  //解密
  decrypt(word, keyStr) {
    if (word == null || word === "") {
      // this.$message.error("无法对空字符串进行解密");
      return;
    }
    keyStr = keyStr ? keyStr : 'abcdefgabcdefg12';
    var key = CryptoJS.enc.Utf8.parse(keyStr);
    var decrypt = CryptoJS.AES.decrypt(word, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
    return CryptoJS.enc.Utf8.stringify(decrypt).toString();
  }

}
